<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>蒸汽压计算器</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #ffffff;
            color: #333;
            min-height: 100vh;
            background-image: linear-gradient(rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0.95)), 
                              url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><rect width="100" height="100" fill="%23f0f8ff"/><path d="M20,20 L80,20 L80,80 L20,80 Z" fill="none" stroke="%23e6f2ff" stroke-width="2"/></svg>');
            background-size: 300px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
            padding: 25px 20px;
            background: linear-gradient(135deg, #1a73e8, #0d47a1);
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            color: white;
            position: relative;
            overflow: hidden;
        }
        
        header::before {
            content: "";
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
            transform: rotate(30deg);
        }
        
        h1 {
            margin: 0;
            font-size: 2.5rem;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
            position: relative;
            z-index: 2;
        }
        
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
            position: relative;
            z-index: 2;
        }
        
        .content {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .controls {
            flex: 1;
            min-width: 300px;
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        }
        
        .chart-container {
            flex: 2;
            min-width: 500px;
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            height: 600px;
            position: relative;
        }
        
        .control-group {
            margin-bottom: 25px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        
        h2 {
            margin-top: 0;
            color: #0d47a1;
            font-size: 1.5rem;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
        }
        
        h2::before {
            content: "";
            display: inline-block;
            width: 5px;
            height: 20px;
            background: #1a73e8;
            margin-right: 10px;
            border-radius: 2px;
        }
        
        .input-group {
            margin-bottom: 18px;
            display: flex;
            align-items: center;
        }
        
        label {
            display: inline-block;
            width: 150px;
            font-weight: 500;
            color: #555;
        }
        
        input, select {
            padding: 12px 15px;
            border-radius: 8px;
            border: 1px solid #ddd;
            background: #f9f9f9;
            color: #333;
            width: 140px;
            font-size: 16px;
            transition: all 0.3s;
        }
        
        input:focus, select:focus {
            outline: none;
            border-color: #1a73e8;
            box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.2);
            background: white;
        }
        
        select {
            margin-left: 10px;
            width: 100px;
            background: white;
        }
        
        button {
            padding: 14px 20px;
            background: linear-gradient(135deg, #1a73e8, #0d47a1);
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: bold;
            font-size: 16px;
            transition: all 0.3s;
            margin-top: 10px;
            width: 100%;
            box-shadow: 0 4px 10px rgba(26, 115, 232, 0.3);
        }
        
        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 7px 15px rgba(26, 115, 232, 0.4);
        }
        
        .result {
            background: #e8f0fe;
            padding: 20px;
            border-radius: 8px;
            margin-top: 20px;
            display: none;
            border-left: 4px solid #1a73e8;
        }
        
        .result.show {
            display: block;
            animation: fadeIn 0.5s;
        }
        
        .result p {
            margin: 12px 0;
            font-size: 1.1rem;
            display: flex;
            justify-content: space-between;
        }
        
        .result-value {
            font-weight: bold;
            color: #0d47a1;
            font-size: 1.2rem;
        }
        
        .legend {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-top: 15px;
            flex-wrap: wrap;
            position: absolute;
            bottom: 20px;
            left: 0;
            right: 0;
        }
        
        .legend-item {
            display: flex;
            align-items: center;
            gap: 8px;
            background: rgba(255, 255, 255, 0.9);
            padding: 6px 12px;
            border-radius: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .legend-color {
            width: 20px;
            height: 4px;
            display: inline-block;
            border-radius: 2px;
        }
        
        .info {
            background: #f5f9ff;
            padding: 20px;
            border-radius: 8px;
            margin-top: 20px;
            font-size: 0.95rem;
            line-height: 1.6;
            border-left: 4px solid #4caf50;
        }
        
        .info h3 {
            color: #2e7d32;
            margin-bottom: 10px;
        }
        
        .info p {
            margin-bottom: 10px;
        }
        
        .info ul {
            padding-left: 20px;
            margin-bottom: 10px;
        }
        
        .info li {
            margin-bottom: 8px;
        }
        
        .formula {
            font-family: monospace;
            background: #f1f8e9;
            padding: 10px;
            border-radius: 5px;
            display: inline-block;
            margin: 5px 0;
        }
        
        @media (max-width: 900px) {
            .content {
                flex-direction: column;
            }
            
            .chart-container {
                min-width: 100%;
                height: 500px;
            }
        }
        
        footer {
            text-align: center;
            padding: 20px;
            color: #666;
            font-size: 0.9rem;
            margin-top: 20px;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>蒸汽压计算器</h1>
            <p class="subtitle">基于Trouton–Hildebrand–Everett规则的压力-温度关系分析</p>
        </header>
        
        <div class="content">
            <div class="controls">
                <div class="control-group">
                    <h2>初始参数设置</h2>
                    <div class="input-group">
                        <label for="initial-pressure">初始压力 (pb):</label>
                        <input type="number" id="initial-pressure" value="100" min="1" step="0.1">
                        <span>kPa</span>
                    </div>
                    <div class="input-group">
                        <label for="initial-temp">初始温度 (Tb):</label>
                        <input type="number" id="initial-temp" value="100" min="-100" step="1">
                        <span>°C</span>
                    </div>
                    <button id="set-params">设置初始温度和压力</button>
                </div>
                
                <div class="control-group">
                    <h2>计算参数</h2>
                    <div class="input-group">
                        <label for="pressure">压力:</label>
                        <input type="number" id="pressure" placeholder="输入压力">
                        <select id="pressure-unit">
                            <option value="kPa">kPa</option>
                            <option value="mmHg">mmHg</option>
                        </select>
                    </div>
                    <div class="input-group">
                        <label for="temperature">温度:</label>
                        <input type="number" id="temperature" placeholder="输入温度">
                        <span>°C</span>
                    </div>
                    <button id="calculate">计算</button>
                </div>
                
                <div class="result" id="result">
                    <p>计算结果：</p>
                    <p>温度: <span class="result-value" id="result-temp">-</span> °C</p>
                    <p>压力: <span class="result-value" id="result-pressure">-</span> kPa</p>
                    <p>压力: <span class="result-value" id="result-pressure-mmhg">-</span> mmHg</p>
                </div>
                
                <div class="info">
                    <h3>关于T-H-E规则</h3>
                    <p>Trouton–Hildebrand–Everett规则描述了液体蒸气压与温度之间的关系：</p>
                    <p class="formula">ln(p/pb) = -Tb(4.4 + lnTb)(1/T - 1/Tb)</p>
                    <p>其中：</p>
                    <ul>
                        <li>pb = 初始压力 (kPa)</li>
                        <li>Tb = 初始沸点 (K)</li>
                        <li>p = 给定压力 (kPa)</li>
                        <li>T = 给定压力下的蒸气压 (K)</li>
                    </ul>
                </div>
            </div>
            
            <div class="chart-container">
                <canvas id="vapor-chart"></canvas>
                <div class="legend">
                    <div class="legend-item">
                        <div class="legend-color" style="background-color: #FF6384;"></div>
                        <span>T-H-E规则曲线</span>
                    </div>
                    <div class="legend-item">
                        <div class="legend-color" style="background-color: #36A2EB;"></div>
                        <span>水蒸气曲线</span>
                    </div>
                    <div class="legend-item">
                        <div class="legend-color" style="background-color: #4BC0C0;"></div>
                        <span>甲烷曲线</span>
                    </div>
                </div>
            </div>
        </div>
        
        <footer>
            <p>蒸汽压计算器 © 2023 | 基于Trouton-Hildebrand-Everett规则开发</p>
        </footer>
    </div>

    <script>
        // 获取DOM元素
        const initialPressureInput = document.getElementById('initial-pressure');
        const initialTempInput = document.getElementById('initial-temp');
        const pressureInput = document.getElementById('pressure');
        const pressureUnitSelect = document.getElementById('pressure-unit');
        const temperatureInput = document.getElementById('temperature');
        const setParamsButton = document.getElementById('set-params');
        const calculateButton = document.getElementById('calculate');
        const resultDiv = document.getElementById('result');
        const resultTemp = document.getElementById('result-temp');
        const resultPressure = document.getElementById('result-pressure');
        const resultPressureMmhg = document.getElementById('result-pressure-mmhg');
        
        // 图表变量
        let vaporChart;
        let chartData = {
            the: [],
            water: [],
            methane: []
        };
        
        // 初始参数
        let pb = 100; // kPa
        let Tb = 373.15; // K (100°C)
        
        // 单位转换函数
        function kpaToMmHg(kpa) {
            return kpa * 7.50062;
        }
        
        function mmhgToKpa(mmhg) {
            return mmhg / 7.50062;
        }
        
        // 摄氏转开尔文
        function celsiusToKelvin(celsius) {
            return celsius + 273.15;
        }
        
        // 开尔文转摄氏
        function kelvinToCelsius(kelvin) {
            return kelvin - 273.15;
        }
        
        // 计算T-H-E规则曲线上的压力
        function calculateTHEPressure(T, pb, Tb) {
            const ln_p_ratio = -Tb * (4.4 + Math.log(Tb)) * (1/T - 1/Tb);
            return pb * Math.exp(ln_p_ratio);
        }
        
        // 计算水蒸气曲线上的压力
        function calculateWaterPressure(T, pb, Tb) {
            const ln_p_ratio = -Tb * (109/8.314) * (1/T - 1/Tb);
            return pb * Math.exp(ln_p_ratio);
        }
        
        // 计算甲烷曲线上的压力
        function calculateMethanePressure(T, pb, Tb) {
            const ln_p_ratio = -Tb * (73/8.314) * (1/T - 1/Tb);
            return pb * Math.exp(ln_p_ratio);
        }
        
        // 生成曲线数据
        function generateCurveData(pb, Tb) {
            const data = { the: [], water: [], methane: [] };
            
            // 生成温度范围（-50°C 到 Tb+100°C）
            const minTemp = Math.max(-50, kelvinToCelsius(Tb) - 150);
            const maxTemp = kelvinToCelsius(Tb) + 100;
            
            for (let celsius = minTemp; celsius <= maxTemp; celsius += 2) {
                const T = celsiusToKelvin(celsius);
                
                // 只计算合理范围内的值
                if (T > 0) {
                    data.the.push({
                        x: celsius,
                        y: calculateTHEPressure(T, pb, Tb)
                    });
                    
                    data.water.push({
                        x: celsius,
                        y: calculateWaterPressure(T, pb, Tb)
                    });
                    
                    data.methane.push({
                        x: celsius,
                        y: calculateMethanePressure(T, pb, Tb)
                    });
                }
            }
            
            return data;
        }
        
        // 初始化图表
        function initChart() {
            const ctx = document.getElementById('vapor-chart').getContext('2d');
            
            // 销毁现有图表（如果存在）
            if (vaporChart) {
                vaporChart.destroy();
            }
            
            vaporChart = new Chart(ctx, {
                type: 'line',
                data: {
                    datasets: [
                        {
                            label: 'T-H-E规则曲线',
                            data: chartData.the,
                            borderColor: '#FF6384',
                            backgroundColor: 'rgba(255, 99, 132, 0.1)',
                            borderWidth: 3,
                            pointRadius: 0,
                            tension: 0.4
                        },
                        {
                            label: '水蒸气曲线',
                            data: chartData.water,
                            borderColor: '#36A2EB',
                            backgroundColor: 'rgba(54, 162, 235, 0.1)',
                            borderWidth: 2,
                            pointRadius: 0,
                            tension: 0.4
                        },
                        {
                            label: '甲烷曲线',
                            data: chartData.methane,
                            borderColor: '#4BC0C0',
                            backgroundColor: 'rgba(75, 192, 192, 0.1)',
                            borderWidth: 2,
                            pointRadius: 0,
                            tension: 0.4
                        }
                    ]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    interaction: {
                        mode: 'index',
                        intersect: false,
                    },
                    scales: {
                        x: {
                            type: 'linear',
                            position: 'bottom',
                            title: {
                                display: true,
                                text: '温度 (°C)',
                                color: '#333',
                                font: {
                                    size: 14,
                                    weight: 'bold'
                                }
                            },
                            grid: {
                                color: 'rgba(0, 0, 0, 0.05)'
                            },
                            ticks: {
                                color: '#666'
                            }
                        },
                        y: {
                            title: {
                                display: true,
                                text: '压力 (kPa)',
                                color: '#333',
                                font: {
                                    size: 14,
                                    weight: 'bold'
                                }
                            },
                            min: 0,
                            max: 130,
                            grid: {
                                color: 'rgba(0, 0, 0, 0.05)'
                            },
                            ticks: {
                                color: '#666'
                            }
                        }
                    },
                    plugins: {
                        legend: {
                            display: false
                        },
                        tooltip: {
                            backgroundColor: 'rgba(255, 255, 255, 0.9)',
                            titleColor: '#0d47a1',
                            bodyColor: '#333',
                            borderColor: '#ddd',
                            borderWidth: 1,
                            padding: 12,
                            cornerRadius: 8,
                            callbacks: {
                                title: function(tooltipItems) {
                                    return `温度: ${tooltipItems[0].parsed.x.toFixed(2)}°C`;
                                },
                                label: function(context) {
                                    const datasetLabel = context.dataset.label || '';
                                    const value = context.parsed.y;
                                    return `${datasetLabel}: ${value.toFixed(2)} kPa`;
                                }
                            }
                        }
                    }
                }
            });
        }
        
        // 设置初始参数
        function setInitialParams() {
            pb = parseFloat(initialPressureInput.value);
            const celsius = parseFloat(initialTempInput.value);
            Tb = celsiusToKelvin(celsius);
            
            // 生成新数据
            chartData = generateCurveData(pb, Tb);
            
            // 更新图表
            initChart();
            
            // 重置计算区域
            pressureInput.value = '';
            temperatureInput.value = '';
            resultDiv.classList.remove('show');
            
            // 显示成功消息
            const message = document.createElement('div');
            message.innerHTML = `参数已更新！初始压力: ${pb} kPa, 初始温度: ${celsius}°C`;
            message.style.position = 'fixed';
            message.style.bottom = '20px';
            message.style.right = '20px';
            message.style.padding = '12px 20px';
            message.style.backgroundColor = '#4caf50';
            message.style.color = 'white';
            message.style.borderRadius = '8px';
            message.style.zIndex = '1000';
            message.style.boxShadow = '0 4px 10px rgba(0,0,0,0.2)';
            message.style.animation = 'fadeIn 0.3s';
            document.body.appendChild(message);
            
            setTimeout(() => {
                message.style.animation = 'fadeOut 0.5s forwards';
                setTimeout(() => {
                    document.body.removeChild(message);
                }, 500);
            }, 3000);
        }
        
        // 计算结果
        function calculateResult() {
            const pressureUnit = pressureUnitSelect.value;
            let pressureValue = parseFloat(pressureInput.value);
            let temperatureValue = parseFloat(temperatureInput.value);
            
            if (pressureValue && !temperatureValue) {
                // 压力转温度
                if (pressureUnit === 'mmHg') {
                    pressureValue = mmhgToKpa(pressureValue);
                }
                
                // 使用T-H-E规则计算温度
                const ln_p_ratio = Math.log(pressureValue / pb);
                const denominator = -Tb * (4.4 + Math.log(Tb));
                const term = ln_p_ratio / denominator;
                const T = 1 / (term + 1/Tb);
                temperatureValue = kelvinToCelsius(T);
                
                // 更新UI
                temperatureInput.value = temperatureValue.toFixed(2);
                showResult(temperatureValue, pressureValue);
                
            } else if (temperatureValue && !pressureValue) {
                // 温度转压力
                const T = celsiusToKelvin(temperatureValue);
                
                // 使用T-H-E规则计算压力
                const term = -Tb * (4.4 + Math.log(Tb)) * (1/T - 1/Tb);
                pressureValue = pb * Math.exp(term);
                
                // 更新UI
                pressureInput.value = pressureValue.toFixed(2);
                showResult(temperatureValue, pressureValue);
                
            } else {
                alert('请只输入一个值（压力或温度）');
            }
        }
        
        // 显示计算结果
        function showResult(temp, pressure) {
            resultTemp.textContent = temp.toFixed(2);
            resultPressure.textContent = pressure.toFixed(2);
            resultPressureMmhg.textContent = kpaToMmHg(pressure).toFixed(2);
            resultDiv.classList.add('show');
            
            // 在图表上添加标记
            addChartMarker(temp, pressure);
        }
        
        // 在图表上添加标记
        function addChartMarker(temp, pressure) {
            // 移除之前的标记（如果存在）
            if (vaporChart.data.datasets.length > 3) {
                vaporChart.data.datasets.splice(3, 2);
            }
            
            // 添加垂直线
            vaporChart.data.datasets.push({
                label: '温度线',
                data: [
                    {x: temp, y: 0},
                    {x: temp, y: pressure}
                ],
                borderColor: '#FF9800',
                backgroundColor: 'rgba(255, 152, 0, 0.1)',
                borderWidth: 2,
                pointRadius: 0,
                borderDash: [5, 5]
            });
            
            // 添加水平线
            vaporChart.data.datasets.push({
                label: '压力线',
                data: [
                    {x: -100, y: pressure},
                    {x: temp, y: pressure}
                ],
                borderColor: '#FF9800',
                backgroundColor: 'rgba(255, 152, 0, 0.1)',
                borderWidth: 2,
                pointRadius: 0,
                borderDash: [5, 5]
            });
            
            // 添加点标记
            vaporChart.data.datasets.push({
                label: '计算点',
                data: [{x: temp, y: pressure}],
                borderColor: '#FF5722',
                backgroundColor: '#FF5722',
                borderWidth: 3,
                pointRadius: 6,
                pointHoverRadius: 8
            });
            
            vaporChart.update();
        }
        
        // 初始化页面
        function initPage() {
            // 生成初始曲线数据
            chartData = generateCurveData(pb, Tb);
            
            // 初始化图表
            initChart();
            
            // 添加事件监听器
            setParamsButton.addEventListener('click', setInitialParams);
            calculateButton.addEventListener('click', calculateResult);
        }
        
        // 页面加载完成后初始化
        window.addEventListener('DOMContentLoaded', initPage);
    </script>
</body>
</html>